//
//  WelcomeViewController.swift
//  DSWeibo
//
//  Created by J on 16/4/17.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit
import SDWebImage
class WelcomeViewController: UIViewController {
    //记录底部约束
    var bottomCons :NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //添加子控件
        view.addSubview(bgIV)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        //布局子控件
        iconView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSizeMake(100, 100))
            make.bottom.equalTo(view).offset(-150)
            make.centerX.equalTo(view)
        }
        
        
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.centerX.equalTo(iconView)
        }
        
        
//        设置用户头像
        if let iconUrl = UserAccount.loadAcount()?.avatar_large{
            let url = NSURL(string: iconUrl as String)!
            iconView.sd_setImageWithURL(url)
        }
        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        iconView.snp_updateConstraints { (make) in
            make.bottom.equalTo(view).offset(-350)
            make.centerX.equalTo(view)
        }

        
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue:0), animations: {
            
            self.iconView.layoutIfNeeded()
            self.messageLabel.layoutIfNeeded()

            }) { (Bool) in
                
                //文本动画
                UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { 
                    self.messageLabel.alpha = 1.0
                    }, completion: { (Bool) in
                        print("OK")
                        //去主页
                        NSNotificationCenter.defaultCenter().postNotificationName(XMGSwitchRootViewControllerKey, object: true)
                })
        }
        
        
        
    }
    
    
    //MARK: -懒加载
    private lazy var bgIV:UIImageView  = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView : UIImageView = {
       let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.cornerRadius = 50
        iv.clipsToBounds = true
        return iv
    }()
    
    
    
    private lazy var messageLabel :UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.sizeToFit()
        label.alpha = 0.0
        return label
        
    }()
    
    
    
}
